package com.jiawa.train.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiawa.train.business.domain.po.DailyTrainStation;
import com.jiawa.train.business.domain.po.TrainStation;
import com.jiawa.train.business.mapper.DailyTrainStationMapper;
import com.jiawa.train.business.req.DailyTrainStationQueryReq;
import com.jiawa.train.business.req.DailyTrainStationSaveReq;
import com.jiawa.train.business.resp.DailyTrainStationQueryResp;
import com.jiawa.train.business.service.IDailyTrainStationService;
import com.jiawa.train.business.service.ITrainStationService;
import com.jiawa.train.common.exception.BusinessException;
import com.jiawa.train.common.exception.BusinessExceptionEnum;
import com.jiawa.train.common.resp.PageResp;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 每日车站 服务实现类
 * </p>
 *
 * @author hrz
 * @since 2025-09-07
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class DailyTrainStationServiceImpl extends ServiceImpl<DailyTrainStationMapper, DailyTrainStation> implements IDailyTrainStationService {

    private final ITrainStationService trainStationService;

    @Override
    public PageResp<DailyTrainStationQueryResp> queryList(DailyTrainStationQueryReq req) {
        Page<DailyTrainStation> page = this.lambdaQuery()
                .eq( req.getDate()!=null,DailyTrainStation::getDate, req.getDate())
                .eq(req.getTrainCode()!=null,DailyTrainStation::getTrainCode, req.getTrainCode())
                .orderByDesc(req.getDate()!=null,DailyTrainStation::getDate)
                .orderByAsc(req.getTrainCode()!=null,DailyTrainStation::getTrainCode)
                .page(new Page<>(req.getPage(), req.getSize()));
        List<DailyTrainStation> records = page.getRecords();
        if(ObjectUtil.isEmpty(records)){
            PageResp<DailyTrainStationQueryResp> r = new PageResp<>();
            r.setList(new ArrayList<>());
            r.setTotal(0L);
            return r;
        }
        List<DailyTrainStationQueryResp> respList = BeanUtil.copyToList(records, DailyTrainStationQueryResp.class);
        PageResp<DailyTrainStationQueryResp> r = new PageResp<>();
        r.setList(respList);
        r.setTotal(page.getTotal());
        return r;
    }

    @Override
    public void delete(Long id) {
        if(ObjectUtil.isEmpty(id)){
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        }
        this.removeById(id);
    }

    @Override
    public void saveDailyTrainStation(DailyTrainStationSaveReq req) {
        //2.判断是否是更新
        DailyTrainStation dailyTrainStation = BeanUtil
                .copyProperties(req, DailyTrainStation.class);
        if(ObjectUtil.isNotEmpty(req.getId())){
            //更新
            dailyTrainStation.setUpdateTime(LocalDateTime.now());
            this.updateById(dailyTrainStation);
        }else{
            //新增
            //1.唯一性确认
            DailyTrainStation one = this.lambdaQuery()
                    .eq(DailyTrainStation::getDate, req.getDate())
                    .eq(DailyTrainStation::getTrainCode, req.getTrainCode())
                    .eq(DailyTrainStation::getIndex, req.getIndex())
                    .eq(DailyTrainStation::getName, req.getName())
                    .one();
            if (ObjectUtil.isNotEmpty(one)|| one != null ) {
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_DAILY_TRAIN_STATION_ERROR);
            }
            dailyTrainStation.setCreateTime(LocalDateTime.now());
            dailyTrainStation.setUpdateTime(LocalDateTime.now());
            this.save(dailyTrainStation);
        }
    }

    @Override
    public void genDaily(Date date, String code) {
        //1.先清空本日车次的车站信息
        this.lambdaUpdate()
                .eq(DailyTrainStation::getDate, date)
                .eq(DailyTrainStation::getTrainCode, code)
                .remove();
        //2.查询该车次的车站信息
        List<TrainStation> trainStationList = trainStationService.lambdaQuery()
                .eq(TrainStation::getTrainCode, code)
                .orderByAsc(TrainStation::getIndex)
                .list();
        if(ObjectUtil.isEmpty(trainStationList)){
            log.info("该车次无车站信息");
            return ;
        }
        //3.生成该日的车站信息
        for (TrainStation trainStation : trainStationList) {
            DailyTrainStation dailyTrainStation = BeanUtil.copyProperties(trainStation, DailyTrainStation.class);
            dailyTrainStation.setId(null);
            dailyTrainStation.setDate(date);
            dailyTrainStation.setTrainCode(code);
            dailyTrainStation.setCreateTime(LocalDateTime.now());
            dailyTrainStation.setUpdateTime(LocalDateTime.now());
            this.save(dailyTrainStation);
        }
    }

    @Override
    public long countByTrainCode(Date date, String trainCode) {
        Long count = this.lambdaQuery()
                .eq(DailyTrainStation::getDate, date)
                .eq(DailyTrainStation::getTrainCode, trainCode)
                .count();
        return count == null ? 0 : count.longValue();
    }
}
